//系统工具[不含业务]

var Tools = {
    /**
     * 获取选中的值
     * @param t 返回 array | string
     * @returns {any}
     */
    getCheckedBoxListVal: function (obj, t) {
        var ids = '';
        $(obj?obj:'.list-check-box').each(function(a, b){
            if($(b).is(":checked")){
                if(ids == ''){
                    ids += ''+$(b).val();
                }else{
                    ids += ','+$(b).val();
                }
            }
        });
        return true===t?ids.split(','):ids;
    },
    showValidatePrompt: function (callBack) {
        var start_num = Math.floor(Math.random() * 10);
        var end_num = Math.floor(Math.random() * 10);

        layer.prompt({
            title: start_num+'*'+end_num+'=？ 请输入计算结果。',
            formType: 0, //输入框类型，支持0（文本）默认1（密码）2（多行文本）
            value: '', //初始时的值，默认空字符
            maxlength: 10, //可输入文本的最大长度，默认500
        }, function (value, index, elem) {
            if(start_num*end_num != 1*value){
                return layer.alert('输入错误');
            }
            if(typeof callBack == 'function'){
                callBack();
            }
            layer.close(index);
        });
    }
};

var Submit = {
    is_debug: true,
    is_layer_load: true,//显示加载层
    //单表单
    saveData: function(obj, fn){
        var form = $(obj).parents('form');
        if(form.length !== 1){
            return Console.error('表单不存在');
        }

        var url = $(form).attr('action');
        if(!url){
            return Console.error('请求url不存在');
        }
        var data = $(form).serializeArray();
        if(this.is_debug){
            Console.info(data);
        }

        if(data.length === 0){
            return Console.error('提交空数据');
        }
        var that = this;


        //遮罩
        if(that.is_layer_load){
            var load_index = layer.load(1);
        }
        $.post(url, data, function (res) {
            if(that.is_debug){
                Console.info(res);
            }
            //取消遮罩
            if(that.is_layer_load){
                layer.close(load_index);
            }
            //处理
            if(fn) fn(res);
        }, 'json');


    },
    //多表单
    //todoing
};

//控制台
var Console = {
    error: function(msg){
        return console.error(msg);
    },
    info: function (msg) {
        return console.log(msg);
    }
};